mod application_controller;
mod assets_controller;
mod authorize_controller;
mod club_controller;
mod form_controller;
mod invite_controller;
mod message_controller;
mod registry_controller;
mod user_controller;
mod v2;
use actix_web::{web, HttpRequest, HttpResponse};
use rand::Rng;
use serde_json::json;

use crate::AppResult;

use self::{
    application_controller::configure_application_controller,
    assets_controller::configure_assets_controller,
    authorize_controller::configure_authorize_controller,
    club_controller::configure_club_controller, form_controller::configure_form_controller,
    invite_controller::configure_invite_controller,
    message_controller::configure_message_controller,
    registry_controller::configure_registry_controller, user_controller::configure_user_controller,
    v2::configure_v2_controller,
};

pub async fn send_config() -> AppResult<HttpResponse> {
    Ok(HttpResponse::Ok().json(json!({
        "content":
            format!(
                "{}?",
                "喵".to_string().repeat(rand::thread_rng().gen_range(2..50))
            )
    })))
}

pub fn configure_controller(config: &mut web::ServiceConfig) {
    config.service(
        web::scope("/api")
            .service(web::resource("/config").to(send_config))
            .service(web::scope("/user").configure(configure_user_controller))
            .service(web::scope("/club").configure(configure_club_controller))
            .service(web::scope("/assets").configure(configure_assets_controller))
            .service(web::scope("/form").configure(configure_form_controller))
            .service(web::scope("/application").configure(configure_application_controller))
            .service(web::scope("/invite").configure(configure_invite_controller))
            .service(web::scope("/message").configure(configure_message_controller))
            .service(web::scope("/registry").configure(configure_registry_controller))
            .service(web::scope("/authorize").configure(configure_authorize_controller))
            .service(web::scope("/v2").configure(configure_v2_controller)),
    );
}
pub async fn default_handler(req: HttpRequest) -> HttpResponse {
    HttpResponse::NotFound().json(json!({
        "code":404,
        "msg":format!("no handler for  {}",req.uri())
    }))
}
